<?php
require_once APPLICATION_PATH . '/controllers/LoginController.php';

/**
 * 合同）控制器
 * @author Administrator
 *
 */
class ContractController extends LoginController
{

    public function init()
    {
        parent::init();
    }

    /**
     * 列表
     * @see LoginController::indexAction()
     */
    public function indexAction()
    {    	
    	$contractM = new Application_Model_DbTable_Contract();
    	
    	//删除临时付费项目
        $contractM->deleteTempItem();
        
        //跟进数据
        $isfollowup = $this->getRequest()->getParam("isfollowup");
        
        if($this->getRequest()->isPost()){
        	//获取数据
	    	$portM  = new Application_Model_DbTable_Port();
	    	$custM  = new Application_Model_DbTable_Customer();    	
	    	$userM  = new Application_Model_DbTable_User();
	    	$currM  = new Application_Model_DbTable_Currency();
	    	
	    	$portKV = $portM->getKvIdNameEn();
	    	$custKV = $custM->getKvIdName();
	    	$userKV = $userM->getKVnameid();
	    	$currKV = $currM->getKvIdName();
	    	
	    	$where = "";
	    	if(!empty($isfollowup)){
	    		$nowDate = date("Y-m-d",time());
	    		$where = " `remind_alarm_time` = '{$nowDate}'";
	    	};
	    	
        	$contractM = new Application_Model_DbTable_Contract();
        	$pageM = $this->_helper->Flexgp();
			$data = $contractM->jqfetchAll($pageM,$where);
			$rdata= $data["data"];
			foreach ($rdata as &$d){
				$d["clerk"] 			= $userKV[$d["clerk"]];
				$d["customer"] 			= $custKV[$d["customer"]];
				$d["currency"] 			= $currKV[$d["currency"]];
				$d["port_of_shipmont"] 	= $portKV[$d["port_of_shipmont"]];
				$d["destination"] 		= $portKV[$d["destination"]];
				$d["create_user"] 		= $userKV[$d["create_user"]];
				$d["create_time"] 		= date("Y-m-d H:i:s",$d["create_time"]);
			}
			
			//$data["data"] = $rdata;//这里是没必要的
			$data = $this->view->jqflexgrid($data,"id");
		    $this->_helper->json($data); 
        }else{
        	//获取页面
    		$this->view->title = "合同列表";
        }
    }

	/** 
     * 跟进列表
     * @see LoginController::indexAction()
     */
    public function followupAction()
    {    	
    	$this->view->isfollowup = "index/isfollowup";
    	$this->view->title = "今日需要跟进";
    	$this->render("index");
    }

    
    
    /**
     * 添加
     */
    public function addAction()
    {
    	$this->view->title = "新增合同";
    	$formM = new Application_Form_Base("contract");
    	$contractM = new Application_Model_DbTable_Contract();
    	$enumM = new Application_Model_DbTable_Enumdata();
    	
    	$contractM->setMultiOptions($formM);
    	$enumM->setMultiOptionAll($formM);
    	$formM->setAction($this->view->pagePath . "/contract/edit");
    	
    	if($this->getRequest()->isPost()){
        	//提交数据
        	//json返回的数据
        	$reData = array(
        		"status" => 0,
        		"msg"	 => "失败。"
        	);
        	$data = $this->getRequest()->getParams();
        	if($formM->isValid($data)){
        		$contractM = new Application_Model_DbTable_Contract();
        		$data  = $formM->getValues(); 
        		//$data["id"] = $data["userid"];unset($data["userid"]);
        		$data['create_user'] = $this->_port_id;	//创建人id
	        	$data['create_time'] = time(); 			//创建时间
	        	try {
		        	$result = $contractM->insert($data);
		        	$reData["status"]=1;
		        	$reData["msg"]="成功！";
		        	Application_Service_Msg::write(
		        		"{$this->_user_name}添加合同：{$data["name"]}",
		        		Application_Model_Msg::TYPE_USERDO_LOG
		        	);
	        	}catch (Exception $e){
        			$reData["form"] = $formM->render();
	        		$reData["msg"]="数据库错误！" . $e->getMessage();
	        	}
        	}else{
        		$reData["msg"]="表单填写有误！";
        		$reData["form"] = $formM->render();
        	}
        	$this->_helper->json($reData);
        }else{
        	//获取表单
        	$data = array();
        	$data['create_user'] = $this->_user_id;	//创建人id
        	$data['create_time'] = time(); 			//创建时间
        	$data['temp_state']  = 1; 				//临时
        	
        	$id = $contractM->insert($data);
        	$idE = $formM->getElement("id");
        	$idE->setValue($id);
        	
        	Application_Service_Msg::write(
        		"{$this->_user_name} 准备新建合同：{$id}",
        		Application_Model_Msg::TYPE_USERDO_LOG
        	);
        	
    		$this->view->form = $formM;
        }
        //$this->_helper->layout()->disableLayout();
        //$this->renderScript("zendform.phtml");
    }
    
    /**
     * 编辑
     * @see LoginController::indexAction()
     */
    public function editAction()
    {
    	$this->view->title = "编辑合同";
    	$id = intval($this->getRequest()->getParam("id",0));
    	$formM = new Application_Form_Base("contract");
    	$formM->setAttrib("id", "contract_form");
    	$contractM = new Application_Model_DbTable_Contract();
		$enumM = new Application_Model_DbTable_Enumdata();
    	
    	$contractM->setMultiOptions($formM);
    	$enumM->setMultiOptionAll($formM);
        $formM->setAction($this->view->pagePath . "/contract/edit");
    	
    	if($this->getRequest()->isPost()){
        	//提交数据
        	$reData = array(
        		"status" => 0,
        		"msg"	 => "失败。"
        	);
        	$data = $this->getRequest()->getParams();
        	if($formM->isValid($data)){
        		
        		$data  = $formM->getValues(); 
        		unset($data["userid"]);
        		$data["temp_state"] = 0;
	        	try {
		        	$result = $contractM->update($data, " `id` = {$id} ");
		        	$reData["status"] = 1;
		        	$reData["approve_status"] = "0"; //变成未审核状态
		        	$reData["msg"]="成功！";
		        	Application_Service_Msg::write(
		        		"{$this->_user_name} 修改合同：{$data["id"]}",
		        		Application_Model_Msg::TYPE_USERDO_LOG
		        	);
	        	}catch (Exception $e){
        			$reData["form"] = $formM->render();
	        		$reData["msg"]="数据库错误！" . $e->getMessage();
	        	}
        	}else{
        		$reData["msg"]="表单填写有误！";
        		$reData["form"] = $formM->render();
        	}
        	$this->_helper->json($reData);
        }else{
        	//获取表单
        	$data = $contractM->fetchRow(" `id` = {$id} ");
        	if($data){
        		$formM->setDefaults($data->toArray());
        	}
    		$this->view->form = $formM;
        }
    }
    
	/**
	 * 删除
	 */
	public function delAction()
	{
		$reData = array(
			"status" => 0,
			"msg"    => "参数错误"
		);	    
    	$id = (array)$this->getRequest()->getParam('id');
    	if($id){
			$contractM = new Application_Model_DbTable_Contract();
			$where = $contractM->getAdapter()->quoteInto('`id` IN(?)', $id);
    		if($result = $contractM->delete($where)){
    			$reData = array(
					"status" => 1,
    		 		"msg"    => "成功删除{$result}条数据！"
    		 	);
    		 	$id = implode(",",$id);
    		 	Application_Service_Msg::write(
	        		"{$this->_user_name} 删除合同：{$id}",
	        		Application_Model_Msg::TYPE_USERDO_LOG
	        	);
    		 }
	    }else{
	        	
	    }
	    $this->_helper->json($reData);
   }

	/**
	 * 打印和预览
	 */
	public function printAction(){
		try{
			$contractid = intval($this->getRequest()->getParam("id",0));
			$contractM  = new Application_Model_DbTable_Contract();
			$contractPM = new Application_Model_DbTable_Contractproduct();
			$compyM  = new Application_Model_DbTable_Companyinfo();
			$custoM  = new Application_Model_DbTable_Customer();
			
			$rsContract  = $contractM->fetchRow(" `id` = {$contractid} ");
			$reContractP = $contractPM->fetchAll(" `contract_id` = {$contractid}  ");
			$reCompny = $compyM->fetchRow();
			$reCustom = $custoM->fetchRow(" `id`={$rsContract->customer} ");
			
			$this->view->contract    = $rsContract;
			$this->view->products = $reContractP->toArray();
			$this->view->cominfo  = $reCompny;
			$this->view->customer = $reCustom;
		}
		catch (Exception $e){
			throw $e;
//			die("404");
		}
		$this->_helper->layout()->disableLayout();
	}
   
	/**
	 * 某个合同的产品列表
	 */
	public function listproductAction()
	{
		$contractid = intval($this->getRequest()->getParam("contractid",0));
		$contractprdM = new Application_Model_DbTable_Contractproduct();
		$pageM = $this->_helper->Flexgp();
		$data = $contractprdM->jqfetchAll($pageM," `contract_id` = {$contractid} ");
		$data = $this->view->jqflexgrid($data,"id");
	    $this->_helper->json($data); 
	}
   
	/**
	 * 添加产品
	 */
	public function addproductAction()
	{
		$contractid = intval($this->getRequest()->getParam("contractid",0));
		$formM = new Application_Form_Base("contract_product");
		$contractM = new Application_Model_DbTable_Contractproduct();
    	$contractM->setMultiOptions($formM);
    	
		if($this->getRequest()->isPost())
		{
			//请求数据
			//json返回的数据
        	$reData = array(
        		"status" => 0,
        		"msg"	 => "失败。"
        	);
        	$data = $this->getRequest()->getParams();
        	if($formM->isValid($data)){
        		$data  = $formM->getValues();
	        	try {
		        	$result = $contractM->insert($data);
		        	$reData["status"]=1;
		        	$reData["msg"]="成功！";
	        	}catch (Exception $e){
        			$reData["form"] = $formM->render();
	        		$reData["msg"]="数据库错误！" . $e->getMessage();
	        	}
        	}else{
        		$reData["msg"]="表单填写有误！";
        		$reData["form"] = $formM->render();
        	}
        	$this->_helper->json($reData);
		}else
		{
			//请求页面
			if(!$contractid)die(404);
			$formM->setAction($this->view->pagePath . "/contract/addproduct");
			$quidE = $formM->getElement("contract_id");
			$quidE->setValue($contractid);
			$this->view->form = $formM;
		}
		$this->_helper->layout()->disableLayout();
        $this->renderScript("zendform.phtml");
	}
	
	/**
	 * 编辑产品
	 */
	public function editproductAction()
	{
		$id = intval($this->getRequest()->getParam("id",0));
		$formM = new Application_Form_Base("contract_product");
		$contractM = new Application_Model_DbTable_Contractproduct();
    	$contractM->setMultiOptions($formM);
		$formM->setAction($this->view->pagePath . "/contract/editproduct");
    	
		if($this->getRequest()->isPost())
		{
			//请求数据
			//json返回的数据
        	$reData = array(
        		"status" => 0,
        		"msg"	 => "失败。"
        	);
        	$data = $this->getRequest()->getParams();
        	if($formM->isValid($data)){
        	$data  = $formM->getValues(); 
        		unset($data["id"]);
	        	try {
		        	$result = $contractM->update($data, " `id` = {$id} ");
		        	$reData["status"]=1;
		        	$reData["msg"]="成功！";
	        	}catch (Exception $e){
        			$reData["form"] = $formM->render();
	        		$reData["msg"]="数据库错误！" . $e->getMessage();
	        	}
        	}else{
        		$reData["msg"]="表单填写有误！";
        		$reData["form"] = $formM->render();
        	}
        	$this->_helper->json($reData);
		}else
		{
			//请求页面
			if(!$id)die(404);
			$data = $contractM->fetchRow(" `id` = {$id} ");
        	if($data){
        		$formM->setDefaults($data->toArray());
        	}
			$this->view->form = $formM;
		}
		$this->_helper->layout()->disableLayout();
        $this->renderScript("zendform.phtml");
	}

	/**
	 * 删除产品
	 */
	public function delproductAction()
	{
		$reData = array(
			"status" => 0,
			"msg"    => "参数错误"
		);	    
    	$id = (array)$this->getRequest()->getParam('id');
    	if($id){
			$contractM = new Application_Model_DbTable_Contractproduct();
			$where = $contractM->getAdapter()->quoteInto('`id` IN(?)', $id);
    		if($result = $contractM->delete($where)){
    			$reData = array(
					"status" => 1,
    		 		"msg"    => "成功删除{$result}条数据！"
    		 	);
    		 	$id = implode(",",$id);
    		 	/*Application_Service_Msg::write(
	        		"{$this->_user_name} 删除合同：{$id}",
	        		Application_Model_Msg::TYPE_USERDO_LOG
	        	);*/
    		 }
	    }else{
	        	
	    }
	    $this->_helper->json($reData);
   }
	
}

